package com.lu.pojo.swing;

import javax.swing.*;
import java.awt.*;

/**
 * @author luzhenfang
 * @date   2020 07 13
 * @description 显示图片
 */
public class ImageTest {
    static class ImageComponent extends JComponent {
        private Image image;


        public ImageComponent(Image a) {
            this.image = a;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 300);
        }

        @Override
        protected void paintComponent(Graphics g) {
            if (image == null) return;
            int width = image.getWidth(this);
            int height = image.getWidth(this);
            g.drawImage(image, 0, 0, null);
            // 填充图片
            for (int i = 0; i < getWidth(); i++) {
                for (int j = 0; j < getHeight(); j++) {
                    if (i + j > 0) {
                        g.copyArea(0, 0, width, height, i * (width+10), j * (height+10));
                    }

                }
            }

        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            JFrame jFrame = new JFrame();
            ImageComponent imageComponent = new ImageComponent(new ImageIcon("C:\\Users\\luzhenfang\\Desktop\\655b9226f9384398a081a9ebd174b328.gif").getImage());
            jFrame.add(imageComponent);
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jFrame.setVisible(true);

        });
    }
}

